<template>
  <div class="jump-search-box">
    <div class="top-search flex-row flex-y-center">
      <div class="flex-grow-1 flex-y-center search-header">
        <div @click.stop="toggleShowSelect" class="select-box flex-grow-0 flex-x-center flex-y-center">
          {{selectList[selectIndex]}} <img style="width: .20rem;margin-left: .2rem"
                                           v-src="'/static/img/icon-sort-down.png'" alt="">
          <div v-show="showSelect" class="selectlist">
            <div @click.stop="selectListClick(index)" :class="{'active':selectIndex==index}"
                 v-for="(item,index) in selectList">{{item}}
            </div>
          </div>
        </div>
        <div class="flex-row-1">
          <form @submit.prevent="subMit" class="form flex-row flex-y-center" action=".">
            <div class="flex-grow-0 inputBox">
              <input ref="input"
                     @focus="onFocus"
                     autocomplete="off"
                     v-model="inputVal" type="search">
              <div class="search_icon">
                <img v-src="'/static/img/icon-search.png'" alt="">
              </div>
            </div>
          </form>
        </div>
        <div @click="clearClick" v-if="inputVal" class="close_icon">
          <img v-src="'/static/img/icon-close2.png'" alt="">
        </div>
      </div>
      <div @click="subMit2" class="flex-grow-0 cancel">
        取消
        <!--<input @click="subMit2" class="submit" type="button" value="取消">-->
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "HouseSearch",
    props: {
      sendVal: {
        type: String,
        default: ''
      },
      label: {
        type: String,
        default: ''
      }
    },
    watch: {
      sendVal(val) {
        if (val) {
          this.inputVal = val;
        }
      },
      label(val) {
        if (val == '二手房') {
          this.selectIndex = 0
        }
        if (val == '租房') {
          this.selectIndex = 1
        }
        if (val == '新房') {
          this.selectIndex = 2
        }
      }
    },
    data() {
      return {
        selectIndex: 0,
        selectList: ['二手房', '租房', '新房'],
        showSelect: false,
        inputVal: ''
      }
    },
    methods: {
      selectListClick(index) {
        this.selectIndex = index;
        this.showSelect = false;
      },
      toggleShowSelect() {
        this.showSelect = !this.showSelect;
      },
      onFocus() {
        console.log('focus');
        this.$emit('on-focus')
      },
      setFocus() {
        this.$refs.input.focus()
      },
      subMit() {
        if (this.inputVal) {
          this.$emit('on-submit', this.inputVal, this.selectList[this.selectIndex])
        } else {
          this.$vux.toast.text('搜索关键词不能为空')
        }
      },
      clearClick() {
        this.inputVal = ''
      },
      subMit2() {
        this.$emit('on-cancel', true)
      }
    }
  }
</script>

<style lang="less" scoped>
  .top-search {
    background-color: #35495e;
    height: 1rem;
    padding: 0 .2rem;
  }

  .search-header {
    height: .6rem;
    background: #fff;
    border-radius: 4px;
    position: relative;
    .close_icon {
      width: 0.32rem;
      position: absolute;
      right: 0.15rem;
      top: 50%;
      transform: translateY(-50%);
    }
  }

  .select-box {
    font-size: .28rem;
    width: 84px;
    height: 100%;
    color: #333;
    position: relative;
    .selectlist {
      width: 100%;
      position: absolute;
      left: 0;
      top: .8rem;
      text-align: center;
      border: 1px solid #eee;
      background-color: #fff;
      border-radius: 4px;
      line-height: .48rem;
      padding: .1rem 0;
      .active {
        color: red;
      }
    }
  }

  .jump-search-box {

  }

  .form {
    input {
      border: none;
    }
    .inputBox {
      position: relative;
      height: 0.64rem;
      border-radius: 30px;
      input {
        font-size: 0.26rem;
        width: 100%;
        height: 100%;
        background: transparent;
        padding-left: 0.66rem;
        caret-color: red;
      }
      .search_icon {
        width: 0.3rem;
        position: absolute;
        left: 0.15rem;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }

  .cancel {
    font-size: 0.3rem;
    margin-left: 0.3rem;
    font-weight: 500;
    //color:#3cc51f;
    color: #fff;
  }

</style>